// 公共请求拦截器

import http from "@/api/http";
import {setStore} from "@/api/utils";
import {commonStore} from "@/store/store";
import {dealWithRoute} from "@/router/dealWithRoute";

export class CommonServices {
    /**
     * 获取菜单
     * @returns {Promise<AxiosResponse<any>>}
     */
    static async getMenus() {
        const {data} = await http({
            url: '/api/menu/get_menus',
            method: 'get'
        })
        setStore('routes', data.data.router_data)
        setStore('menus', data.data.menu_data)
        dealWithRoute(data.data.router_data);
        commonStore().changeMenuData();
    }

    /**
     * 获取首页数据
     * @returns {Promise<AxiosResponse<any>>}
     */
    static getIndexData() {
        return http({
            url: '/api/config/get_index_data',
            method: 'get'
        })
    }

    /**
     * QQ登录
     * @param query
     * @returns {Promise<AxiosResponse<any>>}
     */
    static loginByQQ(query) {
        return http({
            url: '/api/admin/login_by_qq',
            data: query,
            method: 'post'
        })
    }

    /**
     * 获取access_token
     * @param query
     * @returns {Promise<AxiosResponse<any>>}
     */
    static getOpenIdByAccessToken(query) {
        return http({
            url: '/api/admin/get_openid_by_access_token',
            data: query,
            method: 'post'
        })
    }

    /**
     * 获取具体账户的权限信息
     * @returns {Promise<AxiosResponse<any>>}
     * @constructor
     */
    static async GetAccountAuths() {
        const {data} = await http({
            url: '/api/auth/auth_list',
            method: 'post'
        })
        setStore('routes',data.data['router_data']);
        setStore('menus',data.data['menu_data']);
        setStore('permissions',data.data['permission']);
        dealWithRoute(data.data['router_data']);
        commonStore().changeMenuData();
    }
}
